/*********************************************************
*  @file    cm_uart_demo.c
*  @brief   ML302 OpenCPU uart APIs example
*  Copyright (c) 2019 China Mobile IOT.
*  All rights reserved.
*  created by XieGangLiang 2019/10/17
********************************************************/
#include "cm_main.h"

#include "atol_all_head.h"


/*----------------------------------------------------------------------------------------------------
# 函数功能 定义文件名称
# 参数介绍
----------------------------------------------------------------------------------------------------*/
const char DevicePrivateFile[] = "DevicePrivateFile";
const char DeviceBaseFile[] = "DeviceBaseFile";





/*----------------------------------------------------------------------------------------------------
# 函数功能 读取文件中的数据
# 参数介绍
----------------------------------------------------------------------------------------------------*/
void atol_firmware_fs_read(const char *filename,unsigned char *buf, int *len) {
	
	int fd;
	int ret;
	
	// 打开对应的文件名，如果没有文件就返回
	fd = cm_fs_fopen(filename,O_RDWR);
    if(fd < 0) {
        cm_printf("[FREAD]file open failed");
		*len = 0;
        return;
    }
	
	// 读取文件中数据
	ret = cm_fs_fread(fd,buf,*len);
    if(ret <0) {
        cm_printf("[FREAD]file read failed\n");
		*len = 0;
        return;
    }
    cm_printf("[FREAD]%s\n",buf);
	
	// 关闭文件
    cm_fs_fclose(fd);
}


/*----------------------------------------------------------------------------------------------------
# 函数功能 写入文件中的数据
# 参数介绍
----------------------------------------------------------------------------------------------------*/
void atol_firmware_fs_write(const char *filename,unsigned char *buf, int *len) {
	
	
	
	
}


/*----------------------------------------------------------------------------------------------------
# 函数功能 启动时读取文件信息，如果没有文件信息就直接反馈null
# 参数介绍
----------------------------------------------------------------------------------------------------*/
void atol_firmware_fs_init(void) {
	
	
	// 1. 初始化结构体信息
	memset(&DevicePrivateInfoStruct, 0 ,sizeof(struct DEVICE_PRIVATE_INFO_STRUCT));
	
	memset(&DeviceBaseInfoStruct, 0 ,sizeof(struct DEVICE_BASE_INFO_STRUCT));

	
    unsigned char buf[256] = {0};

	unsigned int struct_len = sizeof(DevicePrivateInfoStruct);
	unsigned int len = 0;
	
	
	// 2. 读取DEVICE_PRIVATE
	atol_firmware_fs_read(&DevicePrivateFile, buf, &len);
	
	if (struct_len == len) {
		memcpy(&DevicePrivateInfoStruct, buf ,struct_len);
		
	} else if (len == 0){
		// 文件不正确
		
		
	} else {
		
		
	}
	
	struct_len = sizeof(DeviceBaseInfoStruct);
	len = 0;
	
	// 3. 读取DEVICE_BASE
	atol_firmware_fs_read(&DeviceBaseFile, buf, &len);
	
	if (struct_len == len) {
		memcpy(&DeviceBaseInfoStruct, buf ,struct_len);
		
	} else if (len == 0) {
		// 文件不正确
		
		
	} else {
		
		
	}
	
	
	// 4. 如果有不存在的，就返回错误，停止后续的
	
	
	
    
}













